package cn.com.dhc.zzz.back.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.bbs.ba.base.BAAction;
import cn.bbs.ba.base.UserInfo;
import cn.bbs.ba.error.BAErrorMessage;
import cn.bbs.ba.error.DTOViewMsg;
import cn.bbs.ba.factory.BABeanFactory;
import cn.bbs.ba.mi.BAMima;
import cn.bbs.ba.session.SessionDto;
import cn.bbs.ba.session.SessionFactory;
import cn.bbs.ba.util.CommonUntil;
import cn.com.zzz.dao.PPJFDao;
import cn.com.zzz.dao.PPUserInfoDao;
import cn.com.zzz.pk.dto.PkJF;

/**
 * 普通用户登陆
 */
public class LogonHoutaiAction extends BAAction {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		super.execute(mapping, form, request, response);
		DTOViewMsg msg = new DTOViewMsg();
		request.setAttribute("MSG", msg);
		String stralias = CommonUntil.objToString(request.getParameter("username"));
		if (StringUtils.isEmpty(stralias)) {
			return mapping.findForward("houtaidenglu");
		}
		HttpSession  session = request.getSession();
		// 验证码验证
//		String captcha = CommonUntil.objToString(session.getAttribute("captcha"));
//		if (!captcha.equals(request.getParameter("verificationCode"))) {
//			msg.setNeiRong("VerificationCode error!");
//			return mapping.findForward("houtaidenglu");
//		}
		String strpassword = CommonUntil.objToString(request.getParameter("password"));
		// 查询
		UserInfo userInfo = new UserInfo();
		userInfo.setAlias(stralias);
		userInfo.setPASSPORT(BAMima.encry(stralias + strpassword));
//		userInfo.setPASSPORT("52i68h51g3473ei8d0e1e0e4idf17036");
//		userInfo.setKbn("005");
		
		PPUserInfoDao userInfoDao = (PPUserInfoDao)BABeanFactory.getBean("userInfoDao");
		userInfo = userInfoDao.getUser(userInfo);
		
		// 没有查询到的时候
		if (userInfo == null) {
			//baseDto.setErrorMessage("输入的用户名和密码有误！请确认！");
			//JSONArray curArr = JSONArray.fromObject(baseDto);
			//System.out.println("curArr********" + curArr);
			msg.setNeiRong(BAErrorMessage.push("10001"));
		}
		else {
			// 正常或者手机未验证可以正常登陆
			if ("0".equals(userInfo.getSTT()) 
					|| "4".equals(userInfo.getSTT())) {
				PPJFDao jfDao = (PPJFDao)BABeanFactory.getBean("JF"); 
				PkJF jfDto = new PkJF();
				jfDto.setUserNo(userInfo.getUserNo());
				
				PkJF nowJf = jfDao.getJF(jfDto, "0").get(0);
				userInfo.setValue("JF", nowJf);
				
				SessionDto sessionDto = new SessionDto();
				sessionDto.setUserName(userInfo.getAlias());
				sessionDto.setUserId(userInfo.getUserNo());
				sessionDto.setKbn(userInfo.getKbn());
				sessionDto.setMap(userInfo);

				SessionFactory.setSessionDto(request,sessionDto);
				return mapping.findForward("houMain");
			}
			// 暂时冻结
			else if ("1".equals(userInfo.getSTT())){
				msg.setNeiRong(BAErrorMessage.push(0, "10010", getResouceDisplayAt("暂时冻结")));
			}
			// 暂时冻结
			else if ("2".equals(userInfo.getSTT())){
				msg.setNeiRong(BAErrorMessage.push(0, "10010", getResouceDisplayAt("冻结")));
			}
			// 暂时冻结
			else if ("3".equals(userInfo.getSTT())){
				msg.setNeiRong(BAErrorMessage.push(0, "10010", getResouceDisplayAt("销户")));
			}
			// 暂时冻结
			else if ("9".equals(userInfo.getSTT())){
				msg.setNeiRong(BAErrorMessage.push(0, "10010",  getResouceDisplayAt("已删除")));
			}
			if("0".equals(userInfo.getSTT())) {
				msg.setNeiRong("success");
			}
			else if("4".equals(userInfo.getSTT())){
				msg.setNeiRong("shoujiyanzheng");
			}
		}
		return mapping.findForward("houtaidenglu");
	}

}
